package bjtu.pinet.utils;

import org.onlab.packet.IPv4;

/**
 * author:chushuai
 * date:2021.2.13
 */
public class IPUtils {
    public static String longToIP(long longIp) {
        StringBuffer sb = new StringBuffer("");
        // 直接右移24位
        sb.append(String.valueOf((longIp >>> 24)));
        sb.append(".");
        // 将高8位置0，然后右移16位
        sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
        sb.append(".");
        // 将高16位置0，然后右移8位
        sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
        sb.append(".");
        // 将高24位置0
        sb.append(String.valueOf((longIp & 0x000000FF)));
        return sb.toString();
    }

    public static long ipToLong(String strIp) {
        String[]ip = strIp.split("\\.");
        return (Long.parseLong(ip[0]) << 24) + (Long.parseLong(ip[1]) << 16) + (Long.parseLong(ip[2]) << 8) + Long.parseLong(ip[3]);
    }

    public static long toLong(int i){
        long s = i;
        long result = s+1+Integer.MAX_VALUE-Integer.MIN_VALUE;
        return result;
    }

    public static void main(String[] args) {

        long s  = 2886729985L;
        int t = (int)s;
        System.out.println(t);

        long e = Integer.MIN_VALUE + (s -  Integer.MAX_VALUE)- 1;
        System.out.println((int) e );
        System.out.println(toLong(t));
        System.out.println(ipToLong("172.16.1.1"));
        System.out.println(longToIP(s));
    }
}
